package com.jxt.myjxtmanager.ui.presenter;

import android.content.Context;
import android.widget.TextView;

import com.jxt.myjxtmanager.entity.CourseListBean;
import com.jxt.myjxtmanager.ui.inter.CourseInter;
import com.jxt.myjxtmanager.ui.model.CourseMo;
import com.jxt.myjxtmanager.utils.TimeCalculateCourse;
import com.jxt.myjxtmanager.views.CustomProgressDialog;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * 课程安排 Presenter
 * Created by qm on 2017/3/9 0009.
 * * 暂且规定 第一节课 8:30~9:15  第二节 9:25~10:05  第三节 10:25~11:05  第四节 11:15~12:00   午休 12:00~14:00   第五节  14:00~14:45  第六节 14:55~15:40  第七节 15:50~16:35
 */
public class CoursePresenter implements CourseMo.OnFindAllDataListener {
    private final CustomProgressDialog dialog;
    private CourseMo courseMo;
    private CourseInter inter;

    private final long currentTime = System.currentTimeMillis();
    private final long courseOneUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  08:30:00");
    private final long courseOneDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  09:15:00");
    private final long courseTwoUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  09:25:00");
    private final long courseTwoDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  10:05:00");
    private final long courseThreeUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  10:25:00");
    private final long courseThreeDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  11:05:00");
    private final long courseFourUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  11:15:00");
    private final long courseFourDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  12:00:00");
    private final long courseRestUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  12:00:00");
    private final long courseRestDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  14:00:00");
    private final long courseFiveUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  14:00:00");
    private final long courseFiveDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  14:45:00");
    private final long courseSixUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  14:55:00");
    private final long courseSixDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  15:40:00");
    private final long courseSevenUp = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  15:50:00");
    private final long courseSevenDown = TimeCalculateCourse.getFormatTime(TimeCalculateCourse.getFormatTime(System.currentTimeMillis()) + "  16:35:00");

    public CoursePresenter(Context cxt, CourseInter inter) {
        this.inter = inter;
        courseMo = new CourseMo(cxt);
        courseMo.setOnFindAllDevicesListener(this);
        courseMo.getQueryList();

        dialog = new CustomProgressDialog(cxt, "");
//        animUtils = new TranslateAnimUtils(cxt, 4);
    }

    public List<CourseListBean.CourseBean> getDataList() {
        return courseMo.getQueryList();
    }

    public void setTextVelue(TextView course_state, TextView teacher, TextView teacher_phone) {
        if (getDataList() == null || getDataList().size() <= 0) return;


        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));

        switch (mWay) {
            case "2"://周一
                if (getCourseBean() != null) {
                    course_state.setText("当前课程："+getCourseBean().getCourseContentOne());
                    teacher.setText("任课教师："+getCourseBean().getTeacherOneName());
                    teacher_phone.setText("任课教师电话："+getCourseBean().getTeacherOnePhone());
                } else {
                    course_state.setText("正在休息");
                    teacher.setText("教师： -");
                    teacher_phone.setText("手机号：-");

                }
                break;
            case "3"://周二
                if (getCourseBean() != null) {
                    course_state.setText("当前课程："+getCourseBean().getCourseContentTwo());
                    teacher.setText("任课教师："+getCourseBean().getTeacherTwoName());
                    teacher_phone.setText("任课教师电话："+getCourseBean().getTeacherTwoPhone());
                } else {
                    course_state.setText("正在休息");
                    teacher.setText("教师：-");
                    teacher_phone.setText("手机号：-");

                }

                break;
            case "4"://周三
                if (getCourseBean() != null) {
                    course_state.setText("当前课程："+getCourseBean().getCourseContentThree());
                    teacher.setText("任课教师："+getCourseBean().getTeacherThreeName());
                    teacher_phone.setText("任课教师电话："+getCourseBean().getTeacherThreePhone());
                } else {
                    course_state.setText("当前课程：正在休息");
                    teacher.setText("教师：-");
                    teacher_phone.setText("手机号：-");

                }

                break;
            case "5"://周四
                if (getCourseBean() != null) {
                    course_state.setText("当前课程："+getCourseBean().getCourseContentFour());
                    teacher.setText("任课教师："+getCourseBean().getTeacherFourName());
                    teacher_phone.setText("任课教师电话："+getCourseBean().getTeacherFourPhone());
                } else {
                    course_state.setText("正在休息");
                    teacher.setText("教师：-");
                    teacher_phone.setText("手机号：-");

                }

                break;
            case "6"://周五
                if (getCourseBean() != null) {
                    course_state.setText("当前课程："+getCourseBean().getCourseContentFive());
                    teacher.setText("任课教师："+getCourseBean().getTeacherFiveName());
                    teacher_phone.setText("任课教师电话："+getCourseBean().getTeacherFivePhone());
                } else {
                    course_state.setText("正在休息");
                    teacher.setText("教师：-");
                    teacher_phone.setText("手机号：-");

                }

                break;
        }

    }

    private CourseListBean.CourseBean getCourseBean() {

        if (currentTime >= courseOneUp && currentTime <= courseOneDown) {
            return getDataList().get(0);
        } else if (currentTime >= courseTwoUp && currentTime <= courseTwoDown) {
            return getDataList().get(1);
        } else if (currentTime >= courseThreeUp && currentTime <= courseThreeDown) {
            return getDataList().get(2);
        } else if (currentTime >= courseFourUp && currentTime <= courseFourDown) {
            return getDataList().get(3);
        } else if (currentTime > courseRestUp && currentTime < courseRestDown) {
            return getDataList().get(4);
        } else if (currentTime >= courseFiveUp && currentTime <= courseFiveDown) {
            return getDataList().get(5);
        } else if (currentTime >= courseSixUp && currentTime <= courseSixDown) {
            return getDataList().get(6);
        } else if (currentTime >= courseSevenUp && currentTime <= courseSevenDown) {
            return getDataList().get(7);
        }
        return null;
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void RequestData() {
        if (dialog != null) {
            dialog.showDialog();
        }
        courseMo.RequestData();
    }

    @Override
    public void onFindSuccess() {
        inter.setMyAdapter();
    }

    @Override
    public void onFailure() {

    }

    @Override
    public void onError() {

    }
}
